package com.useeinfo.oa.modules.inspection.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.FacilityBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Facility;
import com.useeinfo.oa.modules.inspection.dao.FacilityInspectionDao;
import com.useeinfo.oa.modules.inspection.entity.FacilityInspection;
import com.useeinfo.oa.modules.inspection.entity.InspectionTechnology;
import com.useeinfo.oa.modules.inspection.entity.PlannerInspection;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Date: 2018/12/28 13:00
 */
@Service("facilityInspectionBiz")
public class FacilityInspectionBiz extends CrudBiz<FacilityInspectionDao, FacilityInspection> {

    @Autowired
    private PlannerInspectionBiz plannerInspectionBiz;
    @Autowired
    private FacilityBiz facilityBiz;
    @Autowired
    private InspectionTechnologyBiz inspectionTechnologyBiz;

    public List<FacilityInspection> findByFacilityId(Long facilityId,String serial){
        return dao.findByFacilityId(facilityId,serial);
    }

    /**
     * 保存计划工单的id,设备——工艺list
     * @param facilityInspectionIds
     * @param plannerInspection
     * @return
     */
    public Set<FacilityInspection> findByIds(String facilityInspectionIds, PlannerInspection plannerInspection){
        net.sf.json.JSONArray jsonArray= net.sf.json.JSONArray.fromObject(facilityInspectionIds);
        Set<FacilityInspection> facilityInspectionSet=new HashSet<>();
        for(Object id:jsonArray){
            FacilityInspection facilityInspection=dao.findModel(StringConverters.null2Long(id));
            facilityInspection.setPlannerInspection(plannerInspection);
            facilityInspection.setTempVal(false);
            dao.add(facilityInspection);
            facilityInspectionSet.add(facilityInspection);
        }
        return facilityInspectionSet;

    }

    /**
     * 保存设备数据
     * @param facilityIds
     * @param plannerId
     * @param serial
     * @param request
     * @return
     */
    public ResultDto saveFacility(String facilityIds, String plannerId, String serial, HttpServletRequest request){

        Enterprise enterprise=SessionKeyUtils.getSessionEnterprise(request);
        PlannerInspection plannerInspection = plannerInspectionBiz.findModel(StringConverters.null2Long(plannerId));
        net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(facilityIds);
        for (Object id : jsonArray) {
            FacilityInspection facilityInspection;
            Facility facility = facilityBiz.findModel(StringConverters.null2Long(id));
            List<FacilityInspection> facilityInspectionList = dao.findByFacilityId(StringConverters.null2Long(id), serial);
            if (CollectionUtils.isNotEmpty(facilityInspectionList)) {
                facilityInspection = facilityInspectionList.get(0);
            } else {
                facilityInspection = new FacilityInspection();
                facilityInspection.setTempVal(Boolean.TRUE);
                facilityInspection.setEnterprise(enterprise);
            }
            facilityInspection.setPlannerInspection(plannerInspection);
            facilityInspection.setSerial(serial);
            facilityInspection.setSpec(facility.getType());
            facilityInspection.setFacilityId(StringConverters.null2Long(id));
            facilityInspection.setFacilityName(facility.getName());
            facilityInspection.setFacilityCode(facility.getCode());
            facilityInspection.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
            facilityInspection.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
            facilityInspection.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
            facilityInspection.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
            facilityInspection.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
            facilityInspection.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
            if(facilityInspection.getId()!=null){
                dao.add(facilityInspection);
            }else{
                dao.update(facilityInspection);
            }
        }
        return ResultUtil.success();
    }

    /**
     * 保存工艺数据
     * @param facilityInspectionIds
     * @param maintainId
     * @param plannerId
     * @return
     */
    public ResultDto saveInspection(String facilityInspectionIds, String maintainId, String plannerId){
        InspectionTechnology inspectionTechnology = inspectionTechnologyBiz.findModel(StringConverters.null2Long(maintainId));
        for(String id:facilityInspectionIds.split(",")){
            PlannerInspection plannerInspection = plannerInspectionBiz.findModel(StringConverters.null2Long(plannerId));
            FacilityInspection facilityInspection = dao.findModel(StringConverters.null2Long(id));
            facilityInspection.setPlannerInspection(plannerInspection);
            facilityInspection.setInspectionId(inspectionTechnology.getId());
            facilityInspection.setInspectionName(inspectionTechnology.getName());
            facilityInspection.setInspectionDataContent(inspectionTechnology.getInspectionDataContent());
            dao.update(facilityInspection);
        }

        return ResultUtil.success();

    }

    /**
     * 删除无效数据
     * @return
     */
    public void deleteUselessData() {
        Date yesterdayTimeStamp = DateTimeUtils.getBeginOfDay(new Date());
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("noId","随便写什么");
        queryParam.getSqlMap().put("beforeTimeStamp",DateTimeUtils.formatDateToStringWithTime(yesterdayTimeStamp));
        List<FacilityInspection> facilityInspectionList = this.findList(queryParam);
        if(CollectionUtils.isNotEmpty(facilityInspectionList)){
            for(FacilityInspection facilityInspection : facilityInspectionList){
                this.delete(facilityInspection.getId());
            }
        }
    }

    /**
     * app接口保存设备工艺数据
     * @param plannerId
     * @param facilityId
     * @param maintainId
     * @param enterprise
     */
    public FacilityInspection saveFacilityInspection(String plannerId,String facilityId,String maintainId,Enterprise enterprise){

        PlannerInspection plannerInspection = plannerInspectionBiz.findModel(StringConverters.null2Long(plannerId));
        FacilityInspection facilityInspection=new FacilityInspection();
        Facility facility = facilityBiz.findModel(StringConverters.null2Long(facilityId));
        if(facility==null){
            return null;
        }
        facilityInspection.setPlannerInspection(plannerInspection);
        facilityInspection.setEnterprise(enterprise);
        facilityInspection.setSerial(plannerInspection.getSerial());
        facilityInspection.setFacilityId(StringConverters.null2Long(facilityId));
        facilityInspection.setFacilityName(facility.getName());
        facilityInspection.setFacilityCode(facility.getCode());
        facilityInspection.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
        facilityInspection.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
        facilityInspection.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
        facilityInspection.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
        facilityInspection.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
        facilityInspection.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
        facilityInspection.setSpec(facility.getType());
        //保存工艺数据
        InspectionTechnology inspectionTechnology = inspectionTechnologyBiz.findModel(StringConverters.null2Long(maintainId));
        if(inspectionTechnology==null){
            return null;
        }
        facilityInspection.setInspectionId(inspectionTechnology.getId());
        facilityInspection.setInspectionName(inspectionTechnology.getName());
        facilityInspection.setInspectionDataContent(inspectionTechnology.getInspectionDataContent());
        dao.add(facilityInspection);
        return facilityInspection;

    }

    public void saveCopy(FacilityInspection facilityInspection, String serial){
        FacilityInspection facilityInspection1=new FacilityInspection();
        facilityInspection1.setAreaId(facilityInspection.getAreaId());
        facilityInspection1.setAreaName(facilityInspection.getAreaName());
        facilityInspection1.setInspectionDataContent(facilityInspection.getInspectionDataContent());
        facilityInspection1.setEnterprise(facilityInspection.getEnterprise());
        facilityInspection1.setInspectionName(facilityInspection.getInspectionName());
        facilityInspection1.setInspectionId(facilityInspection.getInspectionId());
        facilityInspection1.setFacilityCatId(facilityInspection.getFacilityCatId());
        facilityInspection1.setFacilityCatName(facilityInspection.getFacilityCatName());
        facilityInspection1.setFacilityCode(facilityInspection.getFacilityCode());
        facilityInspection1.setFacilityGroupCode(facilityInspection.getFacilityGroupCode());
        facilityInspection1.setFacilityGroupId(facilityInspection.getFacilityGroupId());
        facilityInspection1.setFacilityGroupName(facilityInspection.getFacilityGroupName());
        facilityInspection1.setFacilityId(facilityInspection.getFacilityId());
        facilityInspection1.setQRCode(facilityInspection.getQRCode());
        facilityInspection1.setFacilityName(facilityInspection.getFacilityName());
        facilityInspection1.setSpec(facilityInspection.getSpec());
        facilityInspection1.setTempVal(false);
        facilityInspection1.setSerial(serial);
        dao.update(facilityInspection1);




    }

}
